package com.yy.tech.data.provider.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;

/**
 * @author yy
 * @date 2022/4/18
 * @description: TODO
 */
public class FindCondition {

    private String name;
    private String requestUri;
    private String clsName;
    private FindType type;

    @JsonIgnore
    private AntPathMatcher matcher = new AntPathMatcher();

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getRequestUri() {
        return requestUri;
    }

    public void setRequestUri(final String requestUri) {
        this.requestUri = requestUri;
    }

    public String getClsName() {
        return clsName;
    }

    public void setClsName(final String clsName) {
        this.clsName = clsName;
    }

    public FindType getType() {
        return type;
    }

    public void setType(final FindType type) {
        this.type = type;
    }

    public boolean matchBase(SecretRule rule){
        if (!StringUtils.isEmpty(requestUri)&&!StringUtils.isEmpty(rule.getRequestUri())){
            if(!matcher.match(this.requestUri,rule.getRequestUri())){
                return false;
            }
        }
        if (!StringUtils.isEmpty(clsName) && !StringUtils.isEmpty(rule.getClsName())){
            if(!matcher.match(this.clsName,rule.getClsName())){
                return false;
            }
        }
        return true;
    }

    public static FindCondition create(String name){
        return create(name,FindType.encrypt,null,null);
    }

    public static FindCondition create(String name,FindType type){
        return create(name,type,null,null);
    }

    public static FindCondition create(String name,FindType type,String clsName){
        return create(name,type,clsName,null);
    }

    public static FindCondition create(String name,FindType type,String clsName,String requestUri){
        FindCondition condition = new FindCondition();
        condition.clsName=clsName;
        condition.name = name;
        condition.type =type;
        condition.requestUri = requestUri;
        return condition;
    }

    public static enum FindType{
        encrypt,exclude;
    }
}
